﻿using DoNetDrive.Protocol.Door.Door8800.SystemParameter.TCPSetting;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DoNetDrive.Protocol.Door.Test
{
    public partial class TCPDetailForm : frmNodeForm
    {
        public event Action<TCPDetail> TCPChanged;
        public TCPDetailForm(INMain main)
        {
            InitializeComponent();
            mMainForm = main;
            LoadUILanguage();
           // this.FormClosing += TCPDetailForm_FormClosing;
        }
        protected override void frmNodeForm_FormClosing(object sender, FormClosingEventArgs e)
        {

        }

        public override void LoadUILanguage()
        {
            base.LoadUILanguage();
            GetLanguage(Lab_MAC);
            GetLanguage(Lab_Ip);
            GetLanguage(Lab_IPMask);
            GetLanguage(Lab_IPGateway);
            GetLanguage(Lab_DNSBackup);
            GetLanguage(Lab_TCPPort);
            GetLanguage(Lab_UDPPort1);
            GetLanguage(Lab_ServerPort1);
            GetLanguage(Lab_ServerIP);
            GetLanguage(Lab_ServerAddr);
            GetLanguage(Btn_Save);
            GetLanguage(Btn_Exit);
            GetLanguage(Btn_Default);
            SetControlsFont(this.Controls, GetLanguageFont());
        }

        private void TCPDetailForm_Load(object sender, EventArgs e)
        {
            if (frmMain.SelectedTCPDetail != null)
            {
                var tcp = frmMain.SelectedTCPDetail;
                txtIP.Text = tcp.mIP;
                txtMAC.Text = tcp.mMAC;
                txtIPMask.Text = tcp.mIPMask;
                txtIPGateway.Text = tcp.mIPGateway;
                txtDNS.Text = tcp.mDNS;
                txtDNSBackup.Text = tcp.mDNSBackup;
                txtTCPPort.Text = tcp.mTCPPort.ToString();
                txtUDPPort.Text = tcp.mUDPPort.ToString();
                txtServerIP.Text = tcp.mServerIP;
                txtServerAddr.Text = tcp.mServerAddr;
                txtServerPort.Text = tcp.mServerPort.ToString();
            }
        }


        private void Btn_Default_Click(object sender, EventArgs e)
        {
            txtIP.Text = "192.168.1.150";
            txtIPMask.Text = "255.255.255.255";
            txtIPGateway.Text = "192.168.1.1";
            txtDNS.Text = "0.0.0.0";
            txtDNSBackup.Text = "0.0.0.0";
            txtTCPPort.Text = "8000";
            txtUDPPort.Text = "8101";
            txtServerIP.Text = "0.0.0.0";
            txtServerAddr.Text = "0.0.0.0";
            txtServerPort.Text = "9010";
        }

        private void Btn_Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Btn_Save_Click(object sender, EventArgs e)
        {
            TCPDetail tcp = new TCPDetail
            {
                mIP = txtIP.Text,
                mIPMask = txtIPMask.Text,
                mIPGateway = txtIPGateway.Text,
                mMAC = frmMain.SelectedTCPDetail.mMAC,
                mDNS = txtDNS.Text,
                mDNSBackup = txtDNSBackup.Text,
                mUDPPort = Convert.ToInt32(txtUDPPort.Text),
                mTCPPort = Convert.ToInt32(txtTCPPort.Text),
                mServerAddr = txtServerAddr.Text,
                mServerIP = txtServerIP.Text,
                mServerPort = Convert.ToInt32(txtServerPort.Text)
            };
            if (!TCPDetail.CheckMAC(tcp.mMAC))
            {
                MessageBox.Show("MAC地址格式错误");
                return;
            }
            if (!TCPDetail.CheckIP(tcp.mIP))
            {
                MessageBox.Show("IP地址格式错误");
                return;
            }
            if (!TCPDetail.CheckIP(tcp.mIP))
            {
                MessageBox.Show("IP地址格式错误");
                return;
            }
            if (!TCPDetail.CheckIP(tcp.mIPMask))
            {
                MessageBox.Show("子网掩码地址格式错误");
                return;
            }
            if (!TCPDetail.CheckIP(tcp.mIPGateway))
            {
                MessageBox.Show("网关IP地址格式错误");
                return;
            }
            if (tcp.mUDPPort > 65535 || tcp.mUDPPort <= 0)
            {
                MessageBox.Show("UDP端口格式错误");
                return;
            }
            if (tcp.mTCPPort > 65535 || tcp.mTCPPort <= 0)
            {
                MessageBox.Show("TCP端口格式错误");
                return;
            }
            TCPChanged.Invoke(tcp);
            this.Close();
        }
    }
}
